package org.kalai.unifypay.tfb.pay;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.kalai.unifypay.bean.TFBAgentPayReqVO;
import org.kalai.unifypay.enums.AcctTypeEnum;

/**
 * 天付宝单条代付
 * 
 * @author nicholas
 */
@WebServlet("/tfb_agent_pay_single")
public class TFBAgentPayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		PrintWriter pw = response.getWriter();

		Map<String, String> map = new HashMap<String, String>();
		TFBAgentPayReqVO agentPayReqVO = new TFBAgentPayReqVO();
		checkParams(request, map, agentPayReqVO);

		if (!map.containsKey("respDesc")) {
//			map = TFBAgentPayService.agentPay(agentPayReqVO);
		} else {
			map.put("respCode", "01");
		}
		pw.println(map.toString());
	}

	/**
	 * 字段检验
	 * 
	 * @param request
	 * @param map
	 * @param agentPayReqVO
	 */
	private static void checkParams(HttpServletRequest request, Map<String, String> map,
			TFBAgentPayReqVO agentPayReqVO) {
		String sp_serialno = request.getParameter("sp_serialno");
		String tran_amt = request.getParameter("tran_amt");
		String pay_type = request.getParameter("pay_type");
		String acct_name = request.getParameter("acct_name");
		String acct_id = request.getParameter("acct_id");
		String acct_type = request.getParameter("acct_type");
		String mobile = request.getParameter("mobile");
		String bank_name = request.getParameter("bank_name");
		String bank_settle_no = request.getParameter("bank_settle_no");
		String bank_branch_name = request.getParameter("bank_branch_name");
		String business_type = request.getParameter("business_type");
		String business_no = request.getParameter("business_no");
		String memo = request.getParameter("memo");

		Integer tran_amtInt = null;
		Integer acct_typeInt = null;
		Integer pay_typeInt = null;

		if (StringUtils.isBlank(sp_serialno)) {
			map.put("respDesc", "sp_serialno is required");

		} else if (StringUtils.isBlank(tran_amt)) {
			map.put("respDesc", "tran_amt is required");

		} else if (StringUtils.isBlank(acct_name)) {
			map.put("respDesc", "acct_name is required");

		} else if (StringUtils.isBlank(acct_id)) {
			map.put("respDesc", "acct_id is required");

		} else if (StringUtils.isBlank(acct_type)) {
			map.put("respDesc", "acct_type is required");

		} else if (AcctTypeEnum.DuiGong.getType().toString().equals(acct_type)
				&& StringUtils.isBlank(bank_settle_no)) {// 对公账户必传：传开户所在支行的联行号
			map.put("respDesc", "bank_settle_no is required");

		} else if (StringUtils.isBlank(business_type)) {
			map.put("respDesc", "business_type is required");

		} else if (StringUtils.isBlank(memo)) {
			map.put("respDesc", "memo is required");

		}

		if (StringUtils.isNotBlank(acct_type)) {
			acct_typeInt = Integer.valueOf(acct_type);
			agentPayReqVO.setAcct_type(acct_typeInt);
		}
		if (StringUtils.isNotBlank(pay_type)) {
			pay_typeInt = Integer.valueOf(pay_type);
			agentPayReqVO.setPay_type(pay_typeInt);
		} else {
			agentPayReqVO.setPay_type(1); // 付款方式.1-余额支付,2-企业网银,3-垫资支付,缺损值为1
		}
		if (StringUtils.isNotBlank(tran_amt)) {
			if (tran_amt.contains(".")) {
				map.put("respDesc", "tran_amt format is error");
			} else {
				tran_amtInt = Integer.valueOf(tran_amt);
				agentPayReqVO.setTran_amt(tran_amtInt);
			}
		}

		agentPayReqVO.setAcct_id(acct_id);
		agentPayReqVO.setAcct_name(acct_name);
		agentPayReqVO.setBank_branch_name(bank_branch_name);
		agentPayReqVO.setBank_name(bank_name);
		agentPayReqVO.setBank_settle_no(bank_settle_no);
		agentPayReqVO.setBusiness_no(business_no);
		agentPayReqVO.setBusiness_type(business_type);
		agentPayReqVO.setMemo(memo);
		agentPayReqVO.setMobile(mobile);
		agentPayReqVO.setSp_serialno(sp_serialno);

	}

}
